package form

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

// 定义接收数据的结构体
type Login struct {
	// binding:"required"修饰的字段，若接收为空值，则报错，是必须字段
	User     string `form:"username" json:"user" uri:"user" xml:"user" binding:"required"`
	Password string `form:"password" json:"password" uri:"password" xml:"password" binding:"required"`
}

func init() {
	r := gin.Default()
	r.POST("/login/form", func(context *gin.Context) {
		var form Login
		// Bind()默认解析并绑定form格式
		// 根据请求头中content-type自动推断
		if err := context.Bind(&form); err != nil {
			context.JSON(http.StatusBadRequest, gin.H{
				"error": err.Error(),
			})
			return
		}
		if form.User != "root" || form.Password != "admin" {
			context.JSON(http.StatusBadRequest, gin.H{
				"status": "304",
			})
			return
		} else {
			context.String(http.StatusOK, "登录成功")
		}
	})
	r.Run(":8000")
}
